package io.gitee.tziye.jdbc;

import io.gitee.tziye.common.RenaiUtil;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import static io.gitee.tziye.common.Constants.*;

public class ProcedureResult {

    private final Map<String, Object> result;

    public ProcedureResult(Map<String, Object> result) {
        this.result = result;
    }

    public int result() {
        Object rs = result.get(RESULT);
        if (rs == null) {
            return 0;
        }
        return (int) rs;
    }

    public int updated() {
        Object up = result.get(UPDATED);
        if (up == null) {
            return 0;
        }
        return (int) up;
    }

    public <T> T get(Class<T> clazz) {
        List<T> list = list(clazz);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public <T> List<T> list(Class<T> clazz) {
        Object resultSet = result.get(RESULT_SET);
        if (resultSet == null) {
            return Collections.emptyList();
        }
        return RenaiUtil.parseList(RenaiUtil.string(resultSet), clazz);
    }

}
